﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace BEX.DataAccess
{
    public class UserAccount
    {
        public UserAccount()
        {
        }

        private int _accountID;

        public int AccountID
        {
            get
            {
                return _accountID;
            }
        }

        public int SmsRemaining { get; set; }

        public bool IsActive { get; set; }

        public string EmailAddress { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string UserID { get; set; }

        public static System.Data.Linq.Table<ACCOUNT> GetAccountTable()
        {
            var _context = new BTCDataClassesDataContext();
            
            return _context.GetTable<ACCOUNT>();
        }

        public bool Save()
        {
            var res = true;

            var _context = new BTCDataClassesDataContext();

            var _account = (from c in _context.GetTable<ACCOUNT>()
                                                            where c.ACCOUNT_ID == _accountID
                                                            select c).SingleOrDefault();

            if (null == _account)
            {
                var _acc = GetAccountTable();

                var _a = new ACCOUNT();

                //_a.ACCOUNT_ID = int.Newint();
                _a.SMS_REMAINING = 0;
                _a.IS_ACTIVE = true;
                _a.EMAIL = EmailAddress;
                _a.FIRST_NAME = FirstName;
                _a.LAST_NAME = LastName;

                _acc.InsertOnSubmit(_a);
                _acc.Context.SubmitChanges();
            }
            else
            {
                _account.SMS_REMAINING = SmsRemaining;
                _account.IS_ACTIVE = IsActive;
                _account.EMAIL = EmailAddress;
                _account.FIRST_NAME = FirstName;
                _account.LAST_NAME = LastName;

                _context.SubmitChanges();
            }


            return res;
        }

        public static List<UserAccount> GetList()
        {
            var res = new List<UserAccount>();

            var _l = AccountList();

            foreach (ACCOUNT _a in _l)
            {
                var _toAdd = new UserAccount();

                _toAdd._accountID = _a.ACCOUNT_ID;
                _toAdd.EmailAddress = _a.EMAIL;
                _toAdd.FirstName = _a.FIRST_NAME;
                _toAdd.LastName = _a.LAST_NAME;
                _toAdd.IsActive = _a.IS_ACTIVE;
                _toAdd.SmsRemaining = _a.SMS_REMAINING;
                
                res.Add(_toAdd);
            }

            return res;
        }

        private static List<ACCOUNT> AccountList()
        {
            var _context = new BTCDataClassesDataContext();

            return (from acc in _context.GetTable<ACCOUNT>()
                                    select acc).ToList<ACCOUNT>();
        }

        public static UserAccount Get(int AccountID)
        {
            UserAccount res = null;
            var _context = new BTCDataClassesDataContext();


            var _a = (from acc in _context.GetTable<ACCOUNT>()
                                where (acc.ACCOUNT_ID == AccountID)
                                select acc).ToList<ACCOUNT>();

            if (null != _a && _a.Count > 0)
            {
                res = new UserAccount();

                res._accountID = _a[0].ACCOUNT_ID;
                res.EmailAddress = _a[0].EMAIL;
                res.FirstName = _a[0].FIRST_NAME;
                res.IsActive =  _a[0].IS_ACTIVE;
                res.LastName = _a[0].LAST_NAME;
                res.SmsRemaining = _a[0].SMS_REMAINING;
                
            }

            return res;
        }
    }
}
